package com.dlc.demo.daiqianma.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dlc.demo.daiqianma.R;
import com.dlc.demo.daiqianma.iview.ILoginView;
import com.dlc.demo.daiqianma.presenter.LoginPresenter;
import com.tbruyelle.rxpermissions.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;

/**
 * 用户登录Activity
 */
public class LoginActivity extends BaseActivity implements ILoginView {
    @BindView(R.id.etUserName)
    public EditText etUserName;
    @BindView(R.id.etPwd)
    public EditText etPwd;
    @BindView(R.id.tvForgetPwd)
    public TextView tvForgetPwd;
    @BindView(R.id.tvRegNow)
    public TextView tvRegNow;

    private LoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginPresenter = new LoginPresenter();
        loginPresenter.attachView(this);

       //view注入框架
        ButterKnife.bind(this);

        initView();

        //动态获取权限
        requestPermissions();
    }

    private void initView() {
        //下划线
        tvRegNow.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        //抗锯齿
        tvRegNow.getPaint().setAntiAlias(true);
    }

    @OnClick(R.id.btnLogin)
    public void doLogin(View view){
        String userName = etUserName.getText().toString();
        String pwd = etPwd.getText().toString();
        if("".equals(userName.trim())){
            showToast("请输入手机号！");
            return;
        }
        if("".equals(pwd.trim())){
            showToast("请输入密码！");
            return;
        }
        //执行登录操作
        loginPresenter.doLogin(userName,pwd);
    }
    @OnClick(R.id.ivToBack)
    public void toBack(View view){
        finish();
    }

    /**
     * 登录成功，页面跳转
     */
    @Override
    public void loginSuccess() {
        Intent intent = new Intent(this,ApplyForLoanActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        loginPresenter.detachView();
        super.onDestroy();
    }

    private void requestPermissions(){
        //动态申请内存存储权限
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.INTERNET)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (!aBoolean){
                            showToast("请在设置中打开网络权限");
                        }
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("获取网络权限失败，程序可能无法正常使用");
                    }
                });
    }
}

